/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package PhysicCollection;

/**
 *
 * @author danh.ngothanh
 */
public class Bullet {
    
    int bulletx = 0;
    int bullety = 0;
    int tagetx = 0;
    int tagety = 0;
    int la, lb, lc;
    int bullet_speed = 5;
    boolean movedirction = false;
    boolean xdirection = false;
    boolean ydirection = false;
    public Bullet(int bulletx, int bullety, int tagetx, int tagety){
        this.bulletx = bulletx;
        this.bullety = bullety;
        this.tagetx = tagetx;
        this.tagety = tagety;
        createlinefuntion(bulletx, bullety, tagetx, tagety);
    }
     final void createlinefuntion(int bulletx, int bullety, int tagetx, int tagety){
        la = (tagety - bullety);
        lb = (-1)*(tagetx - bulletx);
        lc = ((-1)*(tagety - bullety)*(bulletx) + (tagetx - bulletx)*bullety);
        if(Math.abs(tagetx - bulletx) > Math.abs(tagety - bullety)){
            movedirction = true;
            if(bulletx < tagetx){
                xdirection = true;
            }else{
                bulletx -= 20;
                bullety = recalculatey(bulletx);
            }
        }else{
            movedirction = false;
            if(bullety < tagety){
                ydirection = true;
            }else{
                bullety -= 20;
                bulletx = recalculatex(bullety);
            }
        }

    }

     public void setSpeed(int newspeedinpixel){
        bullet_speed = newspeedinpixel;
     }

     public boolean isHitTarget(int targetposx, int targetposy, int target_radius){
         if((bulletx > targetposx - target_radius) && (bulletx < targetposx + target_radius) && (bullety > targetposy - target_radius) && (bullety < targetposy + target_radius)){
             return true;
         }
        return false;
     }
    public void relocation(){
        if(movedirction){
            if(xdirection){
                bulletx += bullet_speed;
                bullety = recalculatey(bulletx);
            }else{
                bulletx -= bullet_speed;
                bullety = recalculatey(bulletx);
            }
        }else{
            if(ydirection){
                bullety += bullet_speed;
                bulletx = recalculatex(bullety);
            }else{
                bullety -= bullet_speed;
                bulletx = recalculatex(bullety);
            }
        }
    }
    public boolean checkvisible(){
        if((bulletx<0)||(bulletx > 1270)||(bullety<0)||(bullety > 975))
            return false;
        else
            return true;
    }
     public int recalculatey(int newx){
        int returny = 0;
        if(lb != 0)
          returny = (int)((-1)*la*newx - lc)/lb;
        return returny;
    }
    public int recalculatex(int newy){
        int returnx = 0;
        if(la != 0)
                returnx = (int)((-1)*lb*newy- lc)/la;
        return returnx;
    }
    public void recalculate(){
        if(Math.abs(tagetx - bulletx)>Math.abs(tagety - bullety)){
            if(tagetx > bulletx){
                bulletx ++;
            }
            else if(tagetx < bulletx)
                bulletx --;
            //The vao phuong trinh duong thang
            bullety = recalculatey(bulletx);
        }
        else{
            if(tagety > bullety)
                bullety ++;
            else if(tagety < bullety)
                bullety --;
            //The vao phuong trinh duong thang
            tagety = recalculatex(bullety);
        }
    }
    public int getBulletX(){
        return bulletx;
    }
    public int getBulletY(){
        return bullety;
    }

    @Override
    public void finalize() throws Throwable{        
        bulletx = 0;
        bullety = 0;
        la = 0;
        lb = 0;
        lc = 0;
        super.finalize();
    }
    
}
